package com.xsq.newmall01.Controller;

import com.xsq.newmall01.Dao.User;
import com.xsq.newmall01.Service.UserService.UUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    @Qualifier("userservice")
    private UUserService UUserService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        User loginUser = UUserService.login(user);
        if (loginUser != null) {
            // 新增状态检查：如果用户被禁用（status=0），拒绝登录
            if (loginUser.getStatus() == 0) {
                result.put("success", false);
                result.put("message", "该用户已被禁用，请联系管理员");
            } else {
                result.put("success", true);
                result.put("data", loginUser);
                result.put("message", "登录成功");
            }
        } else {
            result.put("success", false);
            result.put("message", "用户名或密码错误");
        }
        return result;
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/update")
    public Map<String, Object> updateUser(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            UUserService.updateUser(user);
            result.put("success", true);
            result.put("message", "信息更新成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "信息更新失败：" + e.getMessage());
        }
        return result;
    }
}